package com.ccrfid.rmc6.service.business.rollcall;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.rollcall.RollcallScheduleEntity;
import com.ccrfid.rmc6.repository.business.rollcall.RollcallScheduleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.List;

@Service
@Transactional
public class RollcallScheduleServiceImpl implements RollcallScheduleService {
    private static final Logger logger = LoggerFactory.getLogger(RollcallScheduleServiceImpl.class);

    @Autowired
    private RollcallScheduleRepository rollcallScheduleRepository;

    @Override
    public List<RollcallScheduleEntity> getScheduleList() {
        return rollcallScheduleRepository.getAllBy();
    }

    @Override
    public RollcallScheduleEntity createSchedule(RollcallScheduleEntity entity) {
        return rollcallScheduleRepository.save(entity);
    }

    @Override
    public RollcallScheduleEntity updateSchedule(Long scheduleId, RollcallScheduleEntity entity) throws AppException {
        RollcallScheduleEntity scheduleEntity = rollcallScheduleRepository.getById(scheduleId);
        if (scheduleEntity != null) {
            scheduleEntity.setScheduleTime(entity.getScheduleTime());
            scheduleEntity.setRemark(entity.getRemark());
            return rollcallScheduleRepository.save(scheduleEntity);
        } else {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
    }

    @Override
    public int deleteSchedules(List<Long> idList) {
        int count = 0;
        for (Long scheduleId : idList) {
            if (rollcallScheduleRepository.existsById(scheduleId)) {
                rollcallScheduleRepository.deleteById(scheduleId);
                count++;
            }
        }
        return count;
    }

    @Override
    public int getSchedulesStatus() {
        List<RollcallScheduleEntity> list = rollcallScheduleRepository.getAllBy();
        if (list == null || list.size() == 0) {
            return 0;
        } else {
            return list.get(0).getEnabled();
        }
    }

    @Override
    public int enableSchedules() {
        int count = 0;
        List<RollcallScheduleEntity> list = rollcallScheduleRepository.getAllBy();
        for (RollcallScheduleEntity entity : list) {
            entity.setEnabled(1);
            rollcallScheduleRepository.save(entity);
            count++;
        }
        return count;
    }

    @Override
    public int disableSchedules() {
        int count = 0;
        List<RollcallScheduleEntity> list = rollcallScheduleRepository.getAllBy();
        for (RollcallScheduleEntity entity : list) {
            entity.setEnabled(0);
            rollcallScheduleRepository.save(entity);
            count++;
        }
        return count;
    }
}
